package problems.practice;

import java.util.Arrays;

/**
 * 48. 旋转图像
 * https://leetcode.cn/problems/rotate-image/
 */
public class T48_1 {
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length < 2 || matrix.length != matrix[0].length) return;
        int n = matrix.length;
        int t;
        // 对圈数循环
        for (int circle = 0; circle < n / 2; circle++) {
            for (int i = circle; i < n - 1 - circle; i++) {
                t = matrix[circle][i];
                matrix[circle][i] = matrix[n - 1 - i][circle];
                matrix[n - 1 - i][circle] = matrix[n - 1 - circle][n - 1 - i];
                matrix[n - 1 - circle][n - 1 - i] = matrix[i][n - 1 - circle];
                matrix[i][n - 1 - circle] = t;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
        new T48_1().rotate(matrix);
        System.out.println(Arrays.deepToString(matrix));
    }
}
